capture log close
clear all
set maxvar 30000
set more off
pause on
# delimit;

/* 
Figure B6
*/

local data "" ;
local output "" ;

local unit_wk "weekly" ;
local unit_mo "monthly" ;
local bw_wk = 91 ;
local bw_2wk = 60 ;
local bw_mo = 21 ;

local bw_mo_start = 18 ;
local bw_wk_start = 90 ;
local int_mo = 6 ;
local int_wk = 12 ;
local int_2wk = 12 ;
local bwshort_wk = 12 ;
local bwshort_2wk = 12 ;
local bwshort_mo = 6 ;

local genr_label	" income-generating" ;
local nogn_label 	" NON-income-generating" ;

/* Control means */
local all_abbr "a" ;
local male_abbr "m" ;
local female_abbr "f" ;

use "prwora_crime_summstats_exactage.dta", clear ;
foreach sample in all male female { ;
	foreach var in `varlist_`sample''  { ;
		summ 	``sample'_abbr'`var' if label == "prec_rd_mean" ;
		local 	``sample'_abbr'`var' = r(mean) ;
	} ;
} ;


/* RD graphs */
foreach sample in all  { ;

foreach time in wk { ;

	use "prwora_crime_collapse`time'_`sample'_exactage.dta", clear ;
	
	foreach type in 
			char genr nogn 
			{ ;
		foreach var of varlist *`type'*
			{ ;
			local t`var' "Any``type'_label' charge, ages 18-38" ;
			} ;
		foreach var of varlist *`type't*
			{ ;
			local t`var' "Number of``type'_label' charges, ages 18-38" ;
			} ;
	} ;
	foreach var of varlist *ahinc*
		{ ;
		local t`var' "Annual likelihood of incarceration, ages 18-38" ;
		} ;
	foreach var of varlist *chinc*
		{ ;
		local t`var' "Ever incarcerated, ages 18-38" ;
		} ;
		
	/* Drop week 0 because it includes dates on either side of cutoff */
	if "`time'" == "wk" { ;
		drop if dob`time'_run==0;
	} ;		
	
	gen rv = dob`time'_run/4.38 ;
	gen byte above_cutoff = (rv >= 0);
	gen rv_X_uc = rv * (1 - above_cutoff);
	gen rv_X_ac = rv * (above_cutoff);

		
		foreach var of varlist 
			n_crime2char_18_38  n_crime2genr_18_38  n_crime2nogn_18_38  n_crime2chinc_18_38
		{;
		
		foreach type in r_`var' { ;
		
			if "`type'"=="r_`var'" { ;
                preserve;
                    use "prwora_crime_collapsemo_`sample'_exactage.dta", replace ;
    				summ `type' if dobmo_run >= -`bwshort_mo' & dobmo_run < 0 [aweight = N`var'] ;			
                restore;
				local d_`var' = ```sample'_abbr'`var'' - r(mean) ;
				replace `type' = `type' + `d_`var'' ;
			} ;
			
			reg `type' above_cutoff rv_X_ac rv_X_uc if abs(dob`time'_run)<`bw_`time'' [aweight = N`type'];
			local point_estimate : display %-4.3f _b[above_cutoff] ;
			local p_value : display %-4.3f 2*abs( ttail(`e(df_r)', abs( _b[above_cutoff] ) / _se[above_cutoff] )) ;
			local N : display %-10.0fc `e(N)' ;

			display "*** `type' ***" ;
			preserve;
			    local time2 mo;
	            use "prwora_crime_collapsemo_`sample'_exactage.dta", replace ;
                if "`type'"=="r_`var'" { ;
                    replace `type' = `type' + `d_`var'' ;
                } ;                            
                gen rv = dobmo_run   ;
                gen byte above_cutoff = (rv >= 0);
                gen rv_X_uc = rv * (1 - above_cutoff);
                gen rv_X_ac = rv * (above_cutoff);
				
                twoway  
                    (scatter `type' rv if abs(rv)<`bw_`time2'' & dob`time2'_run != 0, msymbol(O) mcolor(black) )
                    (function _b[_cons] + _b[rv_X_uc]*x , range(-`bw_`time2'' 0) lcolor(black) lpattern(dash)  )
                    (function _b[_cons] + _b[rv_X_ac]*x + _b[above_cutoff] , range(0 `bw_`time2'') lcolor(black) lpattern(dash)  )
                    , 
                    title("`t`var''`t`sample''", size(medsmall))
                    xtitle("18th birthday relative to PRWORA (months)", size(medsmall)) xsc(r(-`bw_`time2'' `bw_`time2'')) 
                    ytitle("`ytitle_`var''")
                    legend(off) 
                    xline(0) 
                    xscale(r(-`bw_`time2'' `bw_`time2'') ) 
                    xlab(-`bw_`time2'_start'(`int_`time2'')`bw_`time2'_start')
                    ylabel(, nogrid ) 
                    graphregion(color(white))  
                    bgcolor(white)
                    `ttext_`var''
                    `yscale_`var''
                    ;	
                    graph export "`output'/figB6_`var'.png", width(1000) height(750) replace ;	
					graph export "`output'/figB6_`var'.pdf", replace ;
            restore;
			} ;
			} ;
			};

} ;





